#-*-coding:utf-8-*-
# Create your views here.
from models import Furtherinterest
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect
from django.core.paginator import Paginator



"""保存信息"""
def save (request):
    """管理员才有权限"""
    if request.session['level'] != "administrators":
        return HttpResponseRedirect('/')
    if request.method == 'POST':
            new_data = request.POST.copy()
            if new_data.has_key('id'):
                fi = Furtherinterest.objects.get(pk = new_data['id'])
                fi.name = new_data['name']
            else:
                fi = Furtherinterest(
                name = new_data['name'],
                )
            fi.save()
    return HttpResponseRedirect('/filist/')


"""Agents列表"""
def list(request, page = '1'):
    """管理员才有权限"""
    if request.session['level'] != "administrators":
        return HttpResponseRedirect('/')
    fi = Furtherinterest.objects.order_by('id')
    paginator = Paginator(fi, 10)
    try:
        page = int(page)
#        page = int(request.GET.get('page','1'))
        fi = paginator.page(page)
    except:
        return HttpResponseRedirect('/filist', {'level':request.session['level']})

    return render_to_response('filist.html', {
    'paginator':paginator,
    'fi':fi.object_list,
    'is_paginated': paginator.num_pages > 1,
    'has_next': fi.has_next(),
    'has_previous': fi.has_previous(),
    'current_page': page,
    'next_page': page + 1,
    'previous_page': page - 1,
    'pages': paginator.num_pages,
    'page_numbers': range(paginator.num_pages + 1)[1:],
    'hits' : paginator.count,
    'level':request.session['level'],
    })

"""调用修改显示页"""
def edit(request, fi_id):
    """管理员才有权限"""
    if request.session['level'] != "administrators":
        return HttpResponseRedirect('/')
    fi = get_object_or_404(Furtherinterest, pk = fi_id)
    return render_to_response('edit_fi.html', {'fi': fi})


"""删除"""
def del_fi (request, fi_id = ""):
    """管理员才有权限"""
    if request.session['level'] != "administrators":
        return HttpResponseRedirect('/')
    Furtherinterest.objects.filter(pk = fi_id).delete()
    return HttpResponseRedirect('/filist/')







